package com.baidu.health.controller;

import com.baidu.health.mapper.TbAnswerMapper;
import com.baidu.health.mapper.TbAskMapper;
import com.baidu.health.mapper.TbExcelossMapper;
import com.baidu.health.pojo.ResultResponse;
import com.baidu.health.pojo.TbAnswer;
import com.baidu.health.pojo.TbAsk;
import com.baidu.health.pojo.TbExceloss;
import com.baidu.health.utils.FileUpload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

@RestController
@Api(tags = "评测中心")
@CrossOrigin
public class ExcelController {
    @Autowired
    private TbAskMapper tbAskMapper;
    @Autowired
    private TbAnswerMapper tbAnswerMapper;
    @Autowired
    private TbExcelossMapper excelossMapper;
    //excel导入
    @ApiOperation("/excel导入")
    @PostMapping("/importExcel")
    public ResultResponse importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        //下面有判断判断他的后缀是为03版还是07版的
        Workbook workbook=null;
        InputStream inputStream = file.getInputStream();
        //判断后缀，看是否符合
        //String filename = file.getOriginalFilename();
        //boolean b = filename.endsWith(".xlsx");
        if(file.getOriginalFilename().endsWith(".xlsx") || file.getOriginalFilename().endsWith(".xls")){
            if(file.getOriginalFilename().contains(".xlsx")){
                workbook=new XSSFWorkbook(inputStream);
            }else {
                workbook=new HSSFWorkbook(inputStream);
            }
            if(workbook!=null){
                FileUpload fileUpload=new FileUpload();
                String upload = fileUpload.upload(file);
                TbExceloss tbExceloss=new TbExceloss();
                tbExceloss.setExcelossName(file.getOriginalFilename());
                tbExceloss.setExcelossChaining(upload);
                tbExceloss.setCreateTime(new Date());
                excelossMapper.insert(tbExceloss);
                for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                Sheet sheet = workbook.getSheetAt(i);
                if(sheet == null){
                    String s=new String();
                    return ResultResponse.SUCCESS(s);
                }
                //获取开始行
                int firstRowNum = sheet.getFirstRowNum();
                //获取结束行
                int lastRowNum = sheet.getLastRowNum();
                //循环添加所有的行数
                for (int rowNum = firstRowNum+1; rowNum <= lastRowNum; rowNum++) {
                    Row row = sheet.getRow(rowNum);
                    short firstCellNum = row.getFirstCellNum();
                    short lastCellNum=row.getLastCellNum();
                    int lastCellNum2 = row.getPhysicalNumberOfCells();
                    int askId=0;
                    int answer=0;
                    for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {
                        TbAsk tbAsk=new TbAsk();
                        TbAnswer tbAnswer=new TbAnswer();
                        Cell cell = row.getCell(cellNum);
                        answer++;
                        if( cell == null || "".equals(cell) || cell.getCellType()== Cell.CELL_TYPE_BLANK ){
                            System.out.println(("第"+(rowNum+1)+"行,第"+(cellNum+1)+"列为空"));
                            //return ResultResponse.SUCCESS();
                        }else{
                            //System.out.println(cell);
                            if(answer>1){
                                tbAnswer.setAnswerName(cell.toString());
                                tbAnswer.setAskId(askId);
                                tbAnswer.setFactorId(0);
                                tbAnswer.setCreateTime(new Date());
                                System.out.println(cell.toString());
                                tbAnswerMapper.insert(tbAnswer);
                                System.out.println("获取答案:"+tbAnswer.getAnswerId());
                            }else{
                                tbAsk.setAskTopic(cell.toString());
                                tbAsk.setCreateTime(new Date());
                                tbAskMapper.insert(tbAsk);
                                System.out.println("获取题目:"+tbAsk.getAskId());
                                askId=tbAsk.getAskId();
                            }
                        }
                    }
                }
            }
                return ResultResponse.SUCCESS();
            }else{
                return ResultResponse.FAILED(500,"文件为空");
            }
        }else{
           return ResultResponse.FAILED(500,"请输入正确的excel格式");
        }
    }

}
